//
//  ComposeViewController.swift
//  CXCWeibo
//
//  Created by cxc on 15/7/7.
//  Copyright © 2015年 china. All rights reserved.
//

import UIKit
import FFAutoLayout

class ComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    func inputEmoticon(){
        print("输入表情")
    }
    func selectPicture(){
        print("选择照片")
    }
    func close(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor.whiteColor()
        
        prepareNavigationBar()
        prepareTextView()
    }
    private func prepareToolBar() -> UIToolbar{
        let tb = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 44))
        tb.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        let itemSettings = [["imageName": "compose_toolbar_picture", "action": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]
        var items = [UIBarButtonItem]()
        for setting in itemSettings{
           items.append(UIBarButtonItem(imageName: setting["imageName"]!, highlightedImageName: nil, target: nil, actionName: setting["action"]))
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil))
        }
        items.removeLast()
        tb.items = items
        return tb
    }
    private func prepareTextView(){
        view.addSubview(textView)
        textView.text = "分享新鲜事..."
        textView.font = UIFont.systemFontOfSize(18)
        textView.ff_Fill(view)
        
        textView.inputAccessoryView = prepareToolBar()
        textView.alwaysBounceVertical = true
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        placeHolderLaber.text = "分享新鲜事......"
        placeHolderLaber.sizeToFit()
        textView.addSubview(placeHolderLaber)
        placeHolderLaber.ff_AlignInner(ff_AlignType.TopLeft, referView: textView, size: nil, offset: CGPoint(x: 5, y: 8))
    }
    private func prepareNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        navigationItem.rightBarButtonItem?.enabled = false
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 32))
        let label1 = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label1.text = "发微博"
        label1.sizeToFit()
        let label2 = UILabel(color: UIColor(white: 0.0, alpha: 0.5), fontSize: 13)
        label2.text = sharedUserAccount?.name ?? ""
        label2.sizeToFit()
        titleView.addSubview(label1)
        titleView.addSubview(label2)
        
        label1.ff_AlignInner(ff_AlignType.TopCenter, referView: titleView, size: nil)
        label2.ff_AlignInner(ff_AlignType.BottomCenter, referView: titleView, size: nil)
        
        navigationItem.titleView = titleView
    }
// MARK: - 懒加载
    private lazy var textView = UITextView()
    private lazy var placeHolderLaber = UILabel(color: UIColor.lightGrayColor(), fontSize: 18)

}
